package com.google.common.util.concurrent;

import java.util.concurrent.TimeUnit;

public final class Uninterruptibles {
    public static void sleepUninterruptibly(long sleepFor, TimeUnit unit) {
        long end;
        boolean interrupted = false;
        long remainingNanos;
        try {
            remainingNanos = unit.toNanos(sleepFor);
            end = System.nanoTime() + remainingNanos;
            while (true) {
                TimeUnit.NANOSECONDS.sleep(remainingNanos);
                break;
            }
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        } catch (InterruptedException e) {
            interrupted = true;
            remainingNanos = end - System.nanoTime();
        } catch (Throwable th) {
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
